package com.chinacoal.microservice.impl.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.UnionTaskMaintainClient;
import com.chinacoal.microservice.api.model.QueryUtaskParam;
import com.chinacoal.microservice.api.model.UtaskCompleteTaskParam;
import com.chinacoal.microservice.api.model.entity.UtaskInfoEntity;
import com.chinacoal.microservice.impl.service.UtaskInfoService;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;

@RestController
public class TaskWebClientController implements UnionTaskMaintainClient {
	
    @Resource
    private UtaskInfoService utaskInfoService;

    @Override
    public Result<Object> addTask(UtaskInfoEntity utaskInfoEntity) {
    	try {
    		return utaskInfoService.addTask(utaskInfoEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
    }

	@Override
	public Result<Map<String,List<UtaskInfoEntity>>> battchTaskAdd(List<UtaskInfoEntity> utaskInfoList) {
		try {
			Map<String,List<UtaskInfoEntity>> result = utaskInfoService.battchTaskAdd(utaskInfoList);
    		return Result.success(result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> updateTask(UtaskInfoEntity utaskInfoEntity) {
		try {
			utaskInfoService.updateTask(utaskInfoEntity);
    		return Result.success("更新待办成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> updateAchTask(UtaskInfoEntity utaskInfoEntity) {
		try {
			utaskInfoService.updateAchTask(utaskInfoEntity);
    		return Result.success("更新已办成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> battchUpdateTask(List<UtaskInfoEntity> utaskInfoList) {
		try {
			Map<String,List<UtaskInfoEntity>> result = utaskInfoService.battchUpdateTask(utaskInfoList);
    		return Result.success(result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> completeTask(UtaskCompleteTaskParam param) {
		try {
			utaskInfoService.completeTask(param);
    		return Result.success("完成待办成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Map<String,List<String>>> battchCompleteTask(List<UtaskCompleteTaskParam> paramList) {
		try {
			Map<String,List<String>> result = utaskInfoService.battchCompleteTask(paramList);
    		return Result.success(result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> cancelTask(UtaskCompleteTaskParam param) {
		try {
			utaskInfoService.cancelTask(param);
    		return Result.success("取消待办成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> cancelAchTask(UtaskCompleteTaskParam param) {
		try {
			utaskInfoService.cancelAchTask(param);
    		return Result.success("取消已办成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Map<String,List<String>>> battchCancelTask(List<UtaskCompleteTaskParam> paramList) {
		try {
			Map<String,List<String>> result = utaskInfoService.battchCancelTask(paramList);
    		return Result.success(result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> updateTaskState(String sysCode, String sysTaskId, String state) {
		try {
			utaskInfoService.updateTaskState(sysCode, sysTaskId, state);
    		return Result.success("更新待办状态成功");
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<UtaskInfoEntity> getTask(String sysCode, String sysTaskId) {
		try {
			UtaskInfoEntity UtaskInfoEntity = utaskInfoService.getTask(sysCode, sysTaskId);
    		return Result.success(UtaskInfoEntity);
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Page<UtaskInfoEntity>> getTaskInfoList(QueryUtaskParam param) {
		try {
			Page<UtaskInfoEntity> utaskInfoPage = utaskInfoService.getTaskInfoList(param);
    		return Result.success(utaskInfoPage);
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> getTaskInfoListNum(QueryUtaskParam param) {
		try {
			Integer utaskCount = utaskInfoService.getTaskInfoListNum(param);
    		return Result.success(utaskCount);
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> selectTaskCountList(QueryUtaskParam param) {
		try {
			List<Map<String,Object>> result = utaskInfoService.selectTaskCountList(param);
    		return Result.success(result);
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> getTaskState(String sysCode, String sysTaskId) {
		try {
			String result = utaskInfoService.getTaskState(sysCode, sysTaskId);
    		return Result.success(result);
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> collectTask(String userCode, String taskId, String sysCodeFrom, String flag) {
		try {
			utaskInfoService.collectTask(userCode, taskId, sysCodeFrom, flag);
			
			if("0".equals(flag)) {
				return Result.success("取消收藏成功");
			}

			return Result.success("收藏成功");
		} catch (Exception e) {
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

}
